package com.example.waterdispenserm2.activity;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.example.waterdispenserm2.R;
import com.example.waterdispenserm2.util.SPUtils;
import com.example.waterdispenserm2.adapter.LanguageAdapter;
import com.example.waterdispenserm2.base.BasicsActivity;
import com.example.waterdispenserm2.bean.LanguageBean;
import com.example.waterdispenserm2.databinding.ActivityLanguageBinding;

import java.util.ArrayList;
import java.util.List;

public class LanguageActivity extends BasicsActivity<ActivityLanguageBinding> {


    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initView() {

        initLaunguageData();



        //如果有设置则显示出来
        if (!TextUtils.isEmpty(SPUtils.getString(SPUtils.KEY_LANGUAGE))){
            for (int i=0;i<languageList.size();i++){
                if (languageList.get(i).getLocaleName().equals(SPUtils.getString(SPUtils.KEY_LANGUAGE))){
                    adapter.setType(i);
                    adapter.notifyDataSetChanged();
                }
            }
        }


        binding.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                finish();
            }
        });


        binding.lvLanguage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setType(position);
                String localeName = languageList.get(position).getLocaleName();
                SPUtils.putString(SPUtils.KEY_LANGUAGE, localeName);
                Log.d("Language---put", localeName+"===");
                adapter.notifyDataSetChanged();
                switch (SPUtils.getString(SPUtils.KEY_LANGUAGE)){
                    case "zh"://中文
                        binding.tvTitleLang.setText("语言");
                        break;
                    case "en"://英语
                        binding.tvTitleLang.setText("language");
                        break;
                    case "ja"://日本
                        binding.tvTitleLang.setText("言語");
                        break;
                    case "ko"://韩语
                        binding.tvTitleLang.setText("언어");
                        break;
                }
            }
        });



    }

    @Override
    protected ActivityLanguageBinding getViewBinding() {
        return ActivityLanguageBinding.inflate(LayoutInflater.from(this));
    }

    private List<LanguageBean> languageList;
    private LanguageAdapter adapter;

    private void initLaunguageData() {
        languageList = new ArrayList<>();
        //汉语
        languageList.add(new LanguageBean(getString(R.string.zh), "zh"));
        //英语
        languageList.add(new LanguageBean(getString(R.string.en), "en"));
        //日语
        languageList.add(new LanguageBean(getString(R.string.ja), "ja"));
        //韩语
        languageList.add(new LanguageBean(getString(R.string.ko), "ko"));
        adapter = new LanguageAdapter(this,languageList);
        binding.lvLanguage.setAdapter(adapter);
    }


}